package com.jinglin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinglin.entity.enums.ResponseCodeEnum;
import com.jinglin.entity.po.UserVideoSeries;
import com.jinglin.entity.po.UserVideoSeriesVideo;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.exception.BusinessException;
import com.jinglin.mapper.UserVideoSeriesMapper;
import com.jinglin.mapper.UserVideoSeriesVideoMapper;
import com.jinglin.service.UserVideoSeriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinglin.service.UserVideoSeriesVideoService;
import com.jinglin.service.VideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户视频序列归档 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-03-30
 */
@Service
public class UserVideoSeriesServiceImpl extends ServiceImpl<UserVideoSeriesMapper, UserVideoSeries> implements UserVideoSeriesService {
    @Autowired
    private UserVideoSeriesMapper userVideoSeriesMapper;
    @Autowired
    private UserVideoSeriesVideoMapper userVideoSeriesVideoMapper;
    @Autowired
    private UserVideoSeriesVideoService userVideoSeriesVideoService;
    @Autowired
    private VideoInfoService videoInfoService;
    @Override
    @Transactional
    public void saveUserVideoSeries(UserVideoSeries userVideoSeries, String videoIds) {

        if (userVideoSeries.getSeriesId() != null){
            //不为空，则为更新合集基础信息
            this.updateById(userVideoSeries);
        }
        // 如果这个视频有不属于本用户的。
        if (!checkVideoIds(userVideoSeries.getUserId(),videoIds)){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        // 为空，插入。
        // 先新增合集信息,再保存合集的每个视频信息
        userVideoSeries.setUpdateTime(LocalDateTime.now());
        userVideoSeries.setSort(userVideoSeriesMapper.getMaxSort(userVideoSeries.getUserId()) + 1);
        this.save(userVideoSeries);
        this.saveUserVideoSeriesVideos(userVideoSeries.getUserId(),userVideoSeries.getSeriesId(),videoIds);

    }

    @Override
    @Transactional
    public void saveSeriesVideo(UserVideoSeries userVideoSeries, String videoIds) {
        // 先更新合集信息(更新时间)
        LambdaUpdateWrapper<UserVideoSeries> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserVideoSeries::getUpdateTime, LocalDateTime.now());
        this.update(userVideoSeries,wrapper);
        //再插入合集视频
        String[] ids = videoIds.split(",");
        List<UserVideoSeriesVideo> userVideoSeriesVideos = new ArrayList<>();
        int sort = userVideoSeriesVideoMapper.selectMaxSort(userVideoSeries.getSeriesId(), userVideoSeries.getUserId());
        for (String id : ids) {
            UserVideoSeriesVideo seriesVideo = new UserVideoSeriesVideo();
            seriesVideo.setVideoId(id);
            seriesVideo.setSeriesId(userVideoSeries.getSeriesId());
            seriesVideo.setUserId(userVideoSeries.getUserId());
            seriesVideo.setSort(++sort);
            userVideoSeriesVideos.add(seriesVideo);
        }
        userVideoSeriesVideoService.saveBatch(userVideoSeriesVideos);

    }

    private void saveUserVideoSeriesVideos(String userId,String seriesId, String videoIds) {
        String[] ids = videoIds.split(",");
        List<UserVideoSeriesVideo> userVideoSeriesVideos = new ArrayList<>();
        for (String id : ids) {
            UserVideoSeriesVideo seriesVideo = new UserVideoSeriesVideo();
            seriesVideo.setVideoId(id);
            seriesVideo.setSeriesId(seriesId);
            seriesVideo.setUserId(userId);
            seriesVideo.setSort(userVideoSeriesVideoMapper.selectMaxSort(seriesId,userId)+1);
            userVideoSeriesVideos.add(seriesVideo);
        }
        userVideoSeriesVideoService.saveBatch(userVideoSeriesVideos);
    }

    private boolean checkVideoIds(String userId,String videoIds){
        String[] ids = videoIds.split(",");

        boolean flag = true;
        for (String id : ids) {
            LambdaQueryWrapper<VideoInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(VideoInfo::getUserId,userId);
            wrapper.eq(VideoInfo::getVideoId,id);
            VideoInfo one = videoInfoService.getOne(wrapper);
            if (one == null){
                flag = false;
            }
        }
        return flag;
    }
}
